package cheqex.update;

import javax.swing.*;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

import cheqex.update.controllers.AccountHandler;

public class ChangeUsernameView extends JPanel implements ActionListener{
	private JLabel newUsername;
	private JLabel confirmNewUsername;
	
	JTextField newUsernameField;
	JTextField confirmNewUsernameField;
	
	JButton changeUsername;
	JPanel changeUsernamePanel;
	
	public ChangeUsernameView(){
		initialize();
	}
	
	/*	initialize
	 * 	----------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	initialize initializes the components of the ChangeUsernameView	*/
	public void initialize(){
		this.changeUsernamePanel = new JPanel();
		
		this.newUsername =  new JLabel("New Username:");
		this.confirmNewUsername = new JLabel("Confirm Username:");
		
		this.newUsernameField = new JTextField(20);
		newUsernameField.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestChangeUsername();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});


		
		this.confirmNewUsernameField = new JTextField(20);
	    confirmNewUsernameField.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestChangeUsername();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.changeUsername = new JButton("Change Username");
		
		changeUsername.setMnemonic(KeyEvent.VK_D);
	    changeUsername.setActionCommand("changeUsername");
	    changeUsername.setEnabled(true);
	    changeUsername.addActionListener(this);


	    changeUsernamePanel.setLayout(new GridLayout(5,1));
	    changeUsernamePanel.add(newUsername);
	    changeUsernamePanel.add(newUsernameField);
	    changeUsernamePanel.add(confirmNewUsername);
	    changeUsernamePanel.add(confirmNewUsernameField);
	    changeUsernamePanel.add(changeUsername);
	    
	    this.add(changeUsernamePanel);
	    this.setVisible(true);
	    
	    
	}/*		END OF initialize		*/
	
	public void actionPerformed(ActionEvent e) {
		if ("changeUsername".equals(e.getActionCommand())) {
			requestChangeUsername();
		}
	}
	
	/*	requestChangeUsername
	 * 	---------------------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	requestChangeUsername gets the username and confirmation
	 * 	entered by the user and passes on to the controller
	 * 	to verify if the user entered is a valid username.	*/
	private void  requestChangeUsername(){
		String NewUsername = newUsernameField.getText();
		String ConfirmNewUsername = confirmNewUsernameField.getText();
		AccountHandler.VerifyUsername(NewUsername,ConfirmNewUsername);
		newUsernameField.setText(null);
		confirmNewUsernameField.setText(null);
	}
	/*		END OF requestChangeUsername		*/
}
